package com.zy;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

/**
 * @author: 13417
 * @since: 2024/4/15 19:12:17
 * @description:
 */
public class ActivitiBusinessDemo {

    /*
     *   添加业务key 到 Activiti的表 进行业务与Activiti的关联
     */
    @Test
    public void addBusinessKey() {
        // 1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 3.启动流程实例，同时还要指定业务标识businessKey，也就是出差申请单id，这里是1001
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myEvection", "1001");
        // 4.输出processInstance相关属性
        System.out.println("业务id==" + processInstance.getBusinessKey());
    }

    /*
     *   全部流程实例挂起与激活
     * */
    @Test
    public void SuspendAllProcessInstance() {
        // 1.获取流程实例
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 3.获取流程定义的对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();

        // 得到流程定义的实例是否处于暂停状态
        boolean suspended = processDefinition.isSuspended();
        // 获取流程定义的Id
        String definitionId = processDefinition.getId();
        // 判断是否为暂停状态
        if (suspended) {
            // 如果是暂停，可以执行激活操作 ,参数1 ：流程定义id ，参数2：是否激活，参数3：激活时间
            repositoryService.activateProcessDefinitionById(definitionId,
                    true, null);
            System.out.println("流程定义：" + definitionId + ",已激活");
        } else {
            // 如果是激活状态，可以暂停，参数1 ：流程定义id ，参数2：是否暂停，参数3：暂停时间
            repositoryService.suspendProcessDefinitionById(definitionId,
                    true, null);
            System.out.println("流程定义：" + definitionId + ",已挂起");
        }
    }

    /*
     *   单个流程实例挂起与激活
     * */
    @Test
    public void SuspendSingleProcessInstance() {
        // 1.获取流程实例
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取 RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 3.获取流程定义的对象
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("5001")
                .singleResult();

        // 得到当前流程定义的实例是否处于暂停状态
        boolean suspended = processInstance.isSuspended();
        // 获取流程定义的Id
        String definitionId = processInstance.getId();
        // 判断是否为暂停状态
        if (suspended) {
            // 如果是暂停，可以执行激活操作 ,参数1 ：流程定义id ，参数2：是否激活，参数3：激活时间
            runtimeService.activateProcessInstanceById(definitionId);
            System.out.println("单个流程定义：" + definitionId + ",已激活");
        } else {
            // 如果是激活状态，可以暂停，参数1 ：流程定义id ，参数2：是否暂停，参数3：暂停时间
            runtimeService.suspendProcessInstanceById(definitionId);
            System.out.println("单个 流程定义：" + definitionId + ",已挂起");
        }
    }
}
